package com.cow.net.tcp;

import com.csgyy.Constraint;
import com.csgyy.tcp.domain.DeviceValue;
import com.csgyy.tcp.domain.TypeData;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.util.CharsetUtil;

@Sharable
public class ConnectorIdleStateTrigger extends ChannelInboundHandlerAdapter {


    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            IdleState state = ((IdleStateEvent) evt).state();
            if (state == IdleState.WRITER_IDLE) {
                // write heartbeat to server
                DeviceValue.Request.Builder deviceValue  =DeviceValue.Request.newBuilder();
                deviceValue.setPackageType(TypeData.PING); //数据数据类型
                deviceValue.setDataType(TypeData.DATAFDJ); //发动机类型
                deviceValue.setContext("ok");
                deviceValue.setCompanyId(Constraint.companyId);
                ctx.writeAndFlush(deviceValue);
            }
        } else {
            super.userEventTriggered(ctx, evt);
        }
    }
}
